﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MetroDataApi.Models
{
    public class ProjectLayersHelper
    {
        public static List<projectLayer> GetLayers(int prjId)
        {
            using (var db = new MetroDataDataContext())
            {
                var list = from p in db.PrjGeoLayers
                           orderby p.Sequence
                           where p.PrjId == prjId
                           select new projectLayer()
                           {
                               layer = p.Json,
                               order = p.Sequence.Value,
                               prjId = p.PrjId,
                               title = p.LayerName
                           };
                return list.ToList();
            }
        }

        public static void UpdateLayers(int prjId, List<PrjGeoLayers> layers)
        {
        
            using (var db = new MetroDataDataContext())
            {
                var list = db.PrjGeoLayers.Where(p => p.PrjId == prjId);
                db.PrjGeoLayers.DeleteAllOnSubmit(list);
                for (int i = 0; i < layers.Count; i++)
                {
                    db.PrjGeoLayers.InsertAllOnSubmit(layers);
                }
                db.SubmitChanges();
                
            }   
        }
    }
}